<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/10
 * Time: 18:30
 */
namespace app\index\controller;

use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Privilege extends Base
{
    /**
     * 权限首页展示
     * @return mixed
     */
    public function index()
    {
        $id = empty($_GET['id'])?0:$_GET['id'];
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 100 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                if(!empty($_GET['keyword'])){
                    $keyword = $_GET['keyword'];
                    $where['name'] = ['like',"%{$keyword}%"];
                }else{
                    $where['parent_id'] = $id;
                }
                $where['status'] = ['gt',0];
                $list = Db::name("privilege")->where($where)->limit($start,$limit)->select();
                foreach ($list as $k=>$v){
                    $list[$k]['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
                }

                $count = Db::name("privilege")->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        $this->assign(['id'=>$id]);

        /*$data = \app\index\model\Privilege::all();
        $helper = new \app\common\controller\Helper();
        //使用树型结构展示上级权限
        /*$treeArr = $helper->get_tree($data);

        $this->assign([
            "data" => $treeArr
        ]);*/
        return $this->fetch();
    }

    /**
     * 权限添加页面
     * @return \think\response\View
     */
    public function addview()
    {
        $id = empty($_POST['id'])?0:$_POST['id'];
        $privilege = new \app\index\model\Privilege();
        $helper = new \app\common\controller\Helper();
        $data = $privilege->field("id,name,parent_id")->select();
        //使用树型结构展示上级权限
        $treeArr = $helper->get_tree($data);
        $this->assign([
            "treeArr" => $treeArr,
            "token" => $this->buildToken(),
            "id"=>$id
        ]);
        return view("addView");
    }

    /**
     * 权限添加数据
     */
    public function add()
    {
        $this->checkToken();
        $rule = [
//            ["name", "require|unique:Privilege", "请填写权限名称|权限名称已经存在"],
            ["name", "require", "请填写权限名称"],
            ["module_name", "require", "请填写模块名称"],
            ["controller_name", "require", "请填写控制器名称"],
        ];
        $validate = new Validate($rule);
        $request = $this->request->post();
        if (!$validate->check($request)) {
            $this->msg($validate->getError(), "添加信息", "error");
        }

        $privilege = \app\index\model\Privilege::create($request);
        if (!$privilege->id) {
            $this->msg("添加失败", "添加权限", "error");
        }

        if(!empty($request['alias'])){
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            $data['privilege_id'] = $privilege->id;
            $data['name'] = $request['alias'];
            $where['privilege_id'] = $privilege->id;
            $where['uniacid'] = $thisWechat['id'];
            $aliasId = Db::name("privilege_alias")->where($where)->value("id");
            if(empty($aliasId)){
                Db::name("privilege_alias")->insert($data);
            }else{
                Db::name("privilege_alias")->where(['id'=>$aliasId])->update($data);
            }
        }


        $this->successSessionMsg("添加权限成功");
    }

    /**
     * 权限修改展示页面
     * @return \think\response\View
     */
    public function saveView()
    {
        $id = $this->request->post("id");
        $privilege = new \app\index\model\Privilege();
        $helper = new \app\common\controller\Helper();
        $data = $privilege->field("id,name,parent_id")->where(["id" => ["neq", $id]])->select();
        $one = $privilege->where(["id" => $id])->find();

        //查询是否存在当前名称
        $thisWechat = Session::get("thisWechat");
        $where['privilege_id'] = $one['id'];
        $where['uniacid'] = $thisWechat['id'];
        $name = Db::name("privilege_alias")->where($where)->value("name");
        $one['alias'] = $name;


        //使用树型结构展示上级权限
        $treeArr = $helper->get_tree($data);
        $this->assign([
            "treeArr" => $treeArr,
            "token" => $this->buildToken(),
            "data" => $one
        ]);
        return view("saveView");
    }

    /**
     * 修改数据操作
     */
    public function save()
    {
        $this->checkToken();
        $rule = [
//            ["name", "require|unique:Privilege", "请填写权限名称|权限名称已经存在"],
            ["name", "require", "请填写权限名称"],
            ["module_name", "require", "请填写模块名称"],
            ["controller_name", "require", "请填写控制器名称"],
        ];
        $validate = new Validate($rule);
        $request = $this->request->post();

        if (!$validate->check($request)) {
            $this->msg($validate->getError(), "修改信息", "error");
        }
        if (!\app\index\model\Privilege::update($request)) {
            $this->msg("修改失败", "修改信息", "error");
        }

        if(!empty($request['alias'])){
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            $data['privilege_id'] = $request['id'];
            $data['name'] = $request['alias'];
            $where['privilege_id'] = $request['id'];
            $where['uniacid'] = $thisWechat['id'];
            $aliasId = Db::name("privilege_alias")->where($where)->value("id");
            if(empty($aliasId)){
                Db::name("privilege_alias")->insert($data);
            }else{
                Db::name("privilege_alias")->where(['id'=>$aliasId])->update($data);
            }
        }


        $this->successSessionMsg("修改权限成功");
    }

    /**
     * 删除操作
     */
    public function del()
    {
        $id = $this->request->post("id");
        if(!\app\index\model\Privilege::destroy($id)){
            $black['code'] = 202;
            $black['msg'] = "删除失败";
        }
        $black['code'] = 0;
        $black['msg'] = "删除成功";
        return $black;
    }
}